package se.jagvetintedu;

import java.util.ArrayList;

//A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.
//
//Find the largest palindrome made from the product of two 3-digit numbers.

public class Problem4 {

	Boolean isPalindrome(Integer number) {
		String numberAsString = number.toString();
		
		for (int i = 0; i < (numberAsString.length()+1)/2; i++) {
			if (numberAsString.charAt(i) != numberAsString.charAt((numberAsString.length()-1-i))) {
				return false;
			}
		}
		return true;
	}
	
	Integer findLargestPalindrome() {

		ArrayList<Integer> palindromes = new ArrayList<Integer>();
		for (int i = 999; i > 99; i--) {
			for (int j = 999; j > 99; j--) {
				if (isPalindrome(i*j)) palindromes.add(i*j);
			}
		}
		
		Integer max = 0;
		for (Integer p : palindromes) {
			if (p > max) {
				max = p;
			}
		}
		
		return max;
	}

	public static void main(String args[])
	{
		System.out.println("Project Euler, problem 4");
		
		Problem4 solution = new Problem4();
		System.out.println("Largest palindrome as a result of two 3-digit number multiplication: " + solution.findLargestPalindrome());
	}
	
}
